package cn.itcast.exception;

import cn.itcast.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice/*异常处理的类@RestControllerAdvice = @ControllerAdvice + @ResponseBody*/
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result exceptionHandler(Exception e) {
        e.printStackTrace();
//        log.error(e.getMessage());
        String message = e.getMessage();
        return Result.error(message);
    }

    @ExceptionHandler
    public Result exceptionHandler(DeleteNonEmptyException e) {
        log.error(e.getMessage());
        return Result.error(e.getLocalizedMessage());//获取本地化消息（自己指定的消息）
    }
}
